package com.dwb.service;

import com.dwb.entity.SUser;
import com.dwb.entity.SUserAddress;
import com.dwb.mappers.SUserAddressMapper;
import com.dwb.mappers.SUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author manke
 * @Date 2020/6/27 14:16
 * @Version 0.0.1
 */
@Service
public class SUserService {
    @Autowired
    private SUserMapper sUserMapper;
    @Autowired
    private SUserAddressMapper sUserAddressMapper;

    public PageInfo<SUser> getList(Map<String,String> parameters) {

        PageHelper.startPage(Integer.parseInt(parameters.get("pageNum") == null ? "1" : parameters.get("pageNum")),
                Integer.parseInt(parameters.get("pageSize")==null ? "10" : parameters.get("pageSize")));
        Example example = new Example(SUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("name","%"+(parameters.get("name") == null ? "" : parameters.get("name"))+"%")
                .andLike("mobile","%"+(parameters.get("mobile") == null ? "" : parameters.get("mobile"))+"%");
        List<SUser> sUsers = sUserMapper.selectByExample(example);
        return new PageInfo<>(sUsers);
    }

    public Map<String, Object> getInfo(Map<String, String> parameters) {
        SUser sUser = sUserMapper.selectByPrimaryKey(parameters.get("id"));
        Example example = new Example(SUserAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",sUser.getId());
        List<SUserAddress> sUserAddresses = sUserAddressMapper.selectByExample(example);
        HashMap<String, Object> map = new HashMap<>();
        map.put("sUser",sUser);
        map.put("sUserAddresses",sUserAddresses);
        return map;
    }
}
